package com.gwd.task.model;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.gwd.task.entity.TaskEntity;
import com.gwd.task.group.GroupInsert;
import com.gwd.task.group.GroupUpdate;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhiyongming
 * @date 2020/11/9 10:02
 */
@ToString
@Getter
@Setter
public class TaskDTO {

    @NotNull(message = "任务id不能为空", groups = {GroupUpdate.class})
    @Null(message = "修改时id必须为空", groups = {GroupInsert.class})
    private Long taskId;
    private String taskCode;
    @NotNull(message = "项目id不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private Long projectId;
    private String projectName;
    //模块信息
    @NotNull(message = "模块id不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private Long[] moduleId;
    private String lv1ModuleName;
    private String lv2ModuleName;
    private Long lv1ModuleId;
    private Long lv2ModuleId;
    //开发负责人
    @NotNull(message = "开发负责人不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private Long devPerson;
    private String devPersonName;
    //测试负责人
    @NotNull(message = "测试负责人不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private Long testPerson;
    private String testPersonName;
    //任务分解
    @NotBlank(message = "描述不能为空", groups = {GroupInsert.class, GroupUpdate.class})
    private String decomposition;
    //任务详细描述
    @NotBlank(message = "详情描述不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private String describe;
    //计划完成版本
    @NotNull(message = "计划完成版本不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private Integer completeVersion;
    private String completeVersionName;
    //计划投产时间
    //@NotNull(message = "计划投产时间不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private String commission;
    //计划开发开始时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private String devBegin;
    //计划开发结束时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private String devEnd;
    //调整后开始时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JsonAlias(value = "adjustStart")
    private String realBegin;
    //调整后结束时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JsonAlias(value = "adjustEnd")
    private String realEnd;
    //开发进度
    private String currentProgress;


    //自测情况
    @JsonAlias(value = "selfTest")
    private String self;
    //备注
    private String remarks;
    //创建人
    private Long createUser;
    //所属小组
    @Valid
    @NotNull(message = "组别不能为空", groups = {GroupUpdate.class, GroupInsert.class})
    private Long group;
    //创建时间
    private Date createTime;
    //当前状态
    private Integer status;
    //查询用的多选状态
    private String statusList;
    //操作过程
    private List<TaskProcessDTO> process;

    public TaskDTO(TaskEntity entity) {
        BeanUtils.copyProperties(entity, this);
        this.setCommission(entity.getCommission()==null?"":entity.getCommission().toString());
        this.setDevBegin(entity.getDevBegin()==null?"":entity.getDevBegin().toString());
        this.setDevEnd(entity.getDevEnd()==null?"":entity.getDevEnd().toString());
        this.setRealBegin(entity.getRealBegin()==null?"":entity.getRealBegin().toString());
        this.setRealEnd(entity.getRealEnd()==null?"":entity.getRealEnd().toString());
        this.setModuleId(new Long[]{entity.getLv1ModuleId(), entity.getLv2ModuleId()});
    }

    public void addProcess(TaskProcessDTO dto) {
        if (process==null) {
            process = new ArrayList<>();
        }
        process.add(dto);
    }

    public TaskDTO() {}
}
